package com.appcenter.application.expand;

import com.appcenter.application.dto.apps.AppDto;
import com.appcenter.application.input.app.GetAppsInput;
import com.appcenter.application.input.permission.GetPermissionsServiceInput;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.model.Apps;
import com.appcenter.application.service.DataVersionService;
import com.appcenter.application.service.PermissionService;
import com.appcenter.jz.app.CurrentUser;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Component
public class ObjectFinal {
    public static final String DATA_GENRE = "组织";
    public static final int CODE_EXPIRED = -5;
    public static final String APP_CODE = "JZ";

    @Autowired
    DataVersionService dataVersionService;

    @Autowired
    PermissionService permissionService;

    @Autowired
    AppsMapper appMapper;


    public Long getDataEffectiveVersion() throws Exception {
        return dataVersionService.getDataEffectiveVersion(DATA_GENRE);
    }

    public Boolean editDataEffectiveVersion(Long newVersion) throws Exception {
        return dataVersionService.editDataEffectiveVersion(DATA_GENRE, newVersion);
    }

    //获取拥有权限的AppCode
    public List<String> GetPermissionAppIds(String AppCode, String UserCode) throws Exception {
        GetAppsInput input =new GetAppsInput();
        input.setAppCode(AppCode);
        input.setUserCode(UserCode);
        List<Apps> apps = appMapper.GetApps(input);
        List<String> appIds = new ArrayList<String>();
        if (apps != null && apps.size() > 0) {
            for (int i = 0; i < apps.size(); i++) {
                appIds.add(apps.get(i).getId());
            }
        }
        return appIds;
    }

    //获取拥有权限的AppCode
    public List<String> GetPermissionAppCodes(String AppCode, String UserCode) throws Exception {
        GetAppsInput input =new GetAppsInput();
        input.setAppCode(AppCode);
        input.setUserCode(UserCode);
        List<Apps> apps = appMapper.GetApps(input);
        List<String> appIds = new ArrayList<String>();
        if (apps != null && apps.size() > 0) {
            for (int i = 0; i < apps.size(); i++) {
                appIds.add(apps.get(i).getAppCode());
            }
        }
        return appIds;
    }
    //获取拥有权限的App
    public List<AppDto> GetPermissionApps(String AppCode, String UserCode) throws Exception {
        GetAppsInput input =new GetAppsInput();
        input.setAppCode(AppCode);
        input.setUserCode(UserCode);
        List<Apps> apps = appMapper.GetApps(input);
        List<AppDto> newls = new ArrayList<AppDto>();
        if(apps!=null && apps.size()>0){
            for (int i = 0; i < apps.size(); i++) {
                AppDto appDto=new AppDto();
                BeanUtils.copyProperties(apps.get(i), appDto);
                newls.add(appDto);
            }
        }
        return newls;
    }

}
